package com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo;

import com.bazaaroid.client.web.gwt.partnermodule.client.dto.PartnerAddressDTO;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.common.TitleCommandBar;
import com.bazaaroid.client.web.gwt.partnermodule.client.view.partnerinfo.impl.PartnerInfoTabViewImpl;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PartnerAddressEntryView extends PartnerGenericEntryView<PartnerAddressDTO> {

	private TitleCommandBar titleBar;
	private HTML city = new HTML();
	private HTML country = new HTML();
	
	public class EditClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			getPartnerInfoTabView().startEditPartnerAddress(PartnerAddressEntryView.this);
		}
	}
	
	public class DeleteClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			if(Window.confirm("Are you sure you wand to delete '" + getPartnerGenericDTO().getStreet() + " " + getPartnerGenericDTO().getNumber() + "'?")) {
				getPartnerInfoTabView().getPartnerAddressViewHandler().onPartnerGenericEntryDeleted(getPartnerGenericDTO());
				removeFromParent();
			}
		}
	}
	
	public PartnerAddressEntryView(PartnerInfoTabViewImpl partnerInfoTabView, PartnerAddressDTO partnerAddressDTO) {
		super(partnerInfoTabView, partnerAddressDTO);
		
		VerticalPanel partnerAddressViewPanel = new VerticalPanel();
		initWidget(partnerAddressViewPanel);
		
		titleBar = new TitleCommandBar(partnerAddressDTO.getStreet() + " " + partnerAddressDTO.getNumber());
		titleBar.addCommand("edit", new EditClickHandler());
		titleBar.addCommand("delete", new DeleteClickHandler());
		partnerAddressViewPanel.add(titleBar);
		partnerAddressViewPanel.add(city);
		partnerAddressViewPanel.add(country);
		partnerAddressViewPanel.setStyleName("partnerInfoPanel");
		city.setStyleName("partnerInfoDescription");
		country.setStyleName("partnerInfoDescription");
		//titleBar.addStyleName("");
		update();
	}
	
	public void update() {
		titleBar.setTitle(getPartnerGenericDTO().getStreet() + " " + getPartnerGenericDTO().getNumber());
		city.setHTML(getPartnerGenericDTO().getTownship() + ", " + getPartnerGenericDTO().getPostalCode() + ", " + getPartnerGenericDTO().getCity());
		country.setHTML(getPartnerGenericDTO().getState() + ", " + getPartnerGenericDTO().getCountry());
	}

}
